﻿Imports System.Globalization
Imports System.Security.Cryptography
Imports System.Text

Namespace TweeVo
	Public Module Extensions
		' convert unix time to DateTime
		<System.Runtime.CompilerServices.Extension> _
		Public Function EpochToDateTime(ByVal [date] As String) As Date
			Return New Date(1970, 1, 1).AddSeconds(Long.Parse([date].Remove(0, 2), NumberStyles.HexNumber)).ToLocalTime()
		End Function

		' sometimes the start times aren't on the minute...this fixes that
		<System.Runtime.CompilerServices.Extension> _
		Public Function RoundToNearestMinute(ByVal [date] As Date) As Date
			Return If([date].Second > 30, New Date([date].Year, [date].Month, [date].Day, [date].Hour, [date].Minute, 0).AddMinutes(1), [date])
		End Function

		' encryption methods for sensitive data
		<System.Runtime.CompilerServices.Extension> _
		Public Function EncryptString(ByVal s As String) As String
			Return If(String.IsNullOrEmpty(s), String.Empty, Convert.ToBase64String(ProtectedData.Protect(Encoding.Unicode.GetBytes(s), Nothing, DataProtectionScope.CurrentUser)))
		End Function

		<System.Runtime.CompilerServices.Extension> _
		Public Function DecryptString(ByVal s As String) As String
			Return If(String.IsNullOrEmpty(s), String.Empty, Encoding.Unicode.GetString(ProtectedData.Unprotect(Convert.FromBase64String(s), Nothing, DataProtectionScope.CurrentUser)))
		End Function
	End Module
End Namespace